package com.yisu.expressway.utils;

import android.util.Log;

public class HBLog
{
//	private static boolean UI_DEBUG = true;//!HuabanApp.isRelease();
//	private static boolean OUT_TO_FILE = true;
//	private static final String LOG_FILE_NAME = "huaban.log";

	public static final int V = 0x1;
	public static final int D = 0x2;
	public static final int I = 0x3;
	public static final int W = 0x4;
	public static final int E = 0x5;
	public static final int A = 0x6;

	private static boolean isLoggable = false;

	private static int MAX_LEN = 4000;

	public HBLog()
	{
	}

	public static void setIsLoggable(boolean isLoggable) {
		HBLog.isLoggable = isLoggable;
	}

	public static void v(String tag, String msg) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(V, tag, msg);
		}
	}

	public static void v(String tag, String msg, Throwable tr) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(V, tag, msg + "\n" + tr.getStackTrace());
		}
	}

	public static void d(String tag, String msg) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(D, tag, msg);
		}
	}

	public static void d(String tag, String msg, Throwable tr) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(D, tag, msg + "\n" + tr.getStackTrace());
		}
	}

	public static void i(String tag, String msg) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(I, tag, msg);
		}
	}

	public static void i(String tag, String msg, Throwable tr) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(I, tag, msg + "\n" + tr.getStackTrace());
		}
	}

	public static void w(String tag, String msg) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(W, tag, msg);
		}
	}

	public static void w(String tag, String msg, Throwable tr) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(W, tag, msg + "\n" + tr.getStackTrace());
		}
	}

	public static void e(String tag, String msg) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(E, tag, msg);
		}
	}

	public static void e(String tag, String msg, Throwable tr) {
		if (isLoggable && tag != null && msg != null) {
			printDefault(E, tag, msg + "\n" + tr.getStackTrace());
		}
	}

	public static void printDefault(int type, String tag, String msg) {

		int index = 0;
		int countOfSub = msg.length() / MAX_LEN;

		if (countOfSub > 0) {
			for (int i = 0; i < countOfSub; i++) {
				String sub = msg.substring(index, index + MAX_LEN);
				printSub(type, tag, sub);
				index += MAX_LEN;
			}
			printSub(type, tag, msg.substring(index, msg.length()));
		} else {
			printSub(type, tag, msg);
		}
	}

	private static void printSub(int type, String tag, String sub) {
		switch (type) {
			case V:
				Log.v(tag, sub);
				break;
			case D:
				Log.d(tag, sub);
				break;
			case I:
				Log.i(tag, sub);
				break;
			case W:
				Log.w(tag, sub);
				break;
			case E:
				Log.e(tag, sub);
				break;
			case A:
				Log.wtf(tag, sub);
				break;
		}
	}
}
